<?php

class Html extends Core {
    public function output($out = '') {
        return $out;
    }

    protected function open($tag, $attr = array(), $empty = false) {
        $html = '<' . $tag;
        $attr = $this->attr($attr);
        if (!empty($attr)) {
            $html .= ' ' . $attr;
        }
        $html .= ($empty ? ' /' : '') . '>';
        return $html;
    }

    protected function close($tag) {
        return '</' . $tag . '>';
    }

    public function tag($tag, $content = '', $attr = array(), $empty = false) {
        $html = $this->open($tag, $attr, $empty);
        if (!$empty) {
            $html .= $content . $this->close($tag);
    	}
        return $html;
    }

    public function attr($attr) {
        $attributes = array();
        foreach ($attr as $name => $value) {
            if ($value === true) {
                $value = $name;
            } elseif ($value === false) {
                continue;
            }
            $attributes[] = $name . '="' . $value . '"';
        }
        return join(' ', $attributes);
    }    
    
    public function link($text, $url = null, $attr = array()) {
        if (is_null($url)) {
            $url = $text;
        }
        if (!$this->external($url)) {
			$href = Mapper::base() . $url;		
        } else {
        	$href = $url;
        }
        $attr = array_merge(array(
        	'href' => $href
        	),
        	$attr
        );
        return $this->output($this->tag('a', $text, $attr));
    }
    
    public function image($src, $attr = array()) {
        $attr = array_merge(
            array(
                'alt' => '',
                'title' => isset($attr['alt']) ? $attr['alt'] : ''
            ),
            $attr
        );
		if (strpos($src, 'mithos/')) {
			$src = Mapper::base() . $src;
		} elseif (!$this->external($src)) {
        	$src = Mapper::base() . '/app/themes/' . Config::read('theme') . '/images/' . $src;
        }
        $attr['src'] = $src;
        return $this->output($this->tag('img', null, $attr, true));
    }

    public function css($href = '', $attr = array()) {
        if (is_array($href)) {
            $output = '';
            foreach ($href as $tag) {
                $output .= $this->css($tag, $attr) . PHP_EOL;
        	}
        	return $output;
        } else {
            if (!$this->external($href)) {
            	$href = Mapper::base() . '/app/themes/' . Config::read('theme') . '/css/' . File::extension($href, 'css');
    		}
            $attr = array_merge(
                array(
                    'href' => $href,
                    'rel' => 'stylesheet',
                    'type' => 'text/css'
                ),
                $attr
            );
            return $this->output($this->tag('link', null, $attr, true));
    	}
    }
    
    public function element($page, $data = array()) {
    	$path = THEME . Config::read('theme') . DS . 'elements' . DS;
		if (File::exists($path . $page . '.php')) {
    		ob_start();
    		extract($data);
			require_once $path . $page . '.php';
			$content = ob_get_clean();
			return $content;
		}
		return false;
    }    

    public function script($src = '', $attr = array()) {
        if (is_array($src)) {
            $output = '';
            foreach ($src as $tag) {
                $output .= $this->script($tag, $attr) . PHP_EOL;
        	}
        	return $output;
        } else {
            if (!$this->external($src)) {
                $src = Mapper::base() . '/app/themes/' . Config::read('theme') . '/js/' . File::extension($src, 'js');
        	}
            $attr = array_merge(
                array(
                    'src' => $src,
                    'type' => 'text/javascript'
                ),
                $attr
            );
            return $this->output($this->tag('script', null, $attr));
        }
    }   

    public function div($content, $attr = array()) {
        if(!is_array($attr)) {
            $attr = array('class' => $attr);
    	}
        return $this->output($this->tag('div', $content, $attr));
    }

    public function charset($charset = null) {
        if (is_null($charset)) {
            $charset = 'UTF-8';
        }
        $attr = array(
            'http-equiv' => 'Content-type',
            'content' => 'text/view; charset=' . $charset
        );
        return $this->output($this->tag('meta', null, $attr));
    }

    protected function external($url) {
        return preg_match('/^[a-z]+:/', $url);
    }    
}

?>